package cn.zhpdmmz.service.impl;

import cn.zhpdmmz.entity.*;
import cn.zhpdmmz.mapper.*;
import cn.zhpdmmz.service.ICheckInformationService;
import cn.zhpdmmz.utils.CreatPaymentNo;
import cn.zhpdmmz.utils.DateUtils;
import cn.zhpdmmz.vo.DashboardVo;
import cn.zhpdmmz.vo.PaymentVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * (CheckInformation)表服务实现类
 *
 * @author CJW
 * @since 2022-04-04 13:53:31
 */
@Service("checkInformationService")
public class CheckInformationServiceImpl implements ICheckInformationService {
    @Resource
    private CheckInformationMapper checkInformationMapper;
    @Resource
    private HotelRoomMapper hotelRoomMapper;
    @Resource
    private PaymentMapper paymentMapper;

    @Resource
    private HotelRoomTypeMapper hotelRoomTypeMapper;

    @Resource
    private FinanceMapper financeMapper;
    private Payment payment;
    private Finance finance;


    /**
     * 通过ID查询单条数据
     *
     * @param checkInformationId 主键
     * @return 实例对象
     */
    @Override
    public CheckInformation queryById(Integer checkInformationId) {
        return this.checkInformationMapper.queryById(checkInformationId);
    }

    /**
     * 查询指定行
     *
     * @param checkInformation 筛选条件
     * @return 查询结果
     */
    @Override
    public List<CheckInformation> queryAll(CheckInformation checkInformation) {
        return this.checkInformationMapper.queryAll(checkInformation);
    }

    /**
     * 新增数据
     *
     * @param checkInformation 实例对象
     * @return 实例对象
     */
    @Override
    public CheckInformation insert(CheckInformation checkInformation) {
        this.checkInformationMapper.insert(checkInformation);
        return checkInformation;
    }

    /**
     * 修改数据
     *
     * @param checkInformation 实例对象
     * @return 实例对象
     */
    @Override
    public CheckInformation update(CheckInformation checkInformation) {
        this.checkInformationMapper.update(checkInformation);
        return this.queryById(checkInformation.getCheckInformationId());
    }

    /**
     * 通过主键删除数据
     *
     * @param checkInformationId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer checkInformationId) {
        return this.checkInformationMapper.deleteById(checkInformationId) > 0;
    }
    /**
     * 确认支付
     *
     * @param paymentVo 主键
     * @return 是否成功
     */
    @Override
    public boolean confirmPayment(PaymentVo paymentVo) {
        Date payDay = new Date();
        CheckInformation checkInformation;
        HotelRoom hotelRoom;
        checkInformation = this.checkInformationMapper.queryById(paymentVo.getCheckInformationId());
        checkInformation.setPayStatus("已支付");
        this.checkInformationMapper.update(checkInformation);
        hotelRoom = this.hotelRoomMapper.queryTypeByRoom(this.checkInformationMapper.queryById(paymentVo.getCheckInformationId()).getRoomNo());
        hotelRoom.setStatusId(1);
        this.hotelRoomMapper.update(hotelRoom);

        //新建支付表
        payment = new Payment();
        CreatPaymentNo creatPaymentNo = new CreatPaymentNo();
        String paymentNo;
        Double totalAmount,actualTotalAmount,sale_amount;

        paymentNo = creatPaymentNo.newPaymentNo();
        payment.setPaymentNo(paymentNo);
        payment.setPaymentTime(payDay);

        totalAmount = this.checkInformationMapper.queryById(paymentVo.getCheckInformationId()).getTotalAmount();
        payment.setTotalAmount(totalAmount);
        actualTotalAmount = totalAmount - paymentVo.getDiscount();
        payment.setActualTotalAmount(actualTotalAmount);
        payment.setCheckInformationId(paymentVo.getCheckInformationId());
        payment.setPaymentMode(paymentVo.getPaymentMode());
        payment.setPayee(paymentVo.getPayee());

        //插入财务表
        finance = new Finance();
        int monthNo = payDay.getMonth();
        String month = DateUtils.whichMonth(monthNo);
        sale_amount = financeMapper.queryByMonth(month).getSaleAmount();
        int financeId = financeMapper.queryByMonth(month).getFinanceId();
        finance.setFinanceId(financeId);
        finance.setMonth(month);
        finance.setSaleAmount(sale_amount+actualTotalAmount);

        this.financeMapper.update(finance);
        this.paymentMapper.insert(payment);
        return true;
    }
    //缓存
    Map<Integer,String> mapCache = new HashMap<>();
    private Map<Integer,String> createMapCache(Integer i){
        String name = null;
        mapCache.put(i,name);
        return mapCache;
    }
    /**
     * 按顾客类别统计各类别入住人数
     * @return
     */
    @Override
    public List<DashboardVo> countByCustomerType() {
        List<CheckInformation> checkInformationList = this.checkInformationMapper.queryAll(new CheckInformation());
        List<DashboardVo> dashboardVoList = new ArrayList<>();
        String name0 = null;
        int i = 0;
        for (CheckInformation checkInformation : checkInformationList) {
            DashboardVo dashboardVo = new DashboardVo();
            long count = this.checkInformationMapper.countByCustomerType(checkInformation.getCustomerType());
            String name = checkInformation.getCustomerType();
            if(! name.equals(name0)){
                if(i == 0 ){
                    this.createMapCache(i);
                    dashboardVo.setValue(count);
                    dashboardVo.setName(name);
                    dashboardVoList.add(dashboardVo);
                    mapCache.put(i,name);
                    i++;
                    name0 = name;
                }
                else{
                    int n = 0;
                    for(int j = 0; j < i;j++){
                        if(mapCache.get(j).equals(name)){
                            n = 1;
                            break;
                        }
                    }
                    if(n == 0){
                        this.createMapCache(i);
                        dashboardVo.setValue(count);
                        dashboardVo.setName(name);
                        dashboardVoList.add(dashboardVo);
                        mapCache.put(i,name);
                        i++;
                        name0 = name;
                    }
                }
            }
        }
        return dashboardVoList;
    }


    /**
     * 按房间类型id统计各类型订房数
     * @return
     */
    @Override
    public List<DashboardVo> countByTypeId() {
        List<HotelRoomType> hotelRoomTypeList = this.hotelRoomTypeMapper.queryAll(new HotelRoomType());
        List<DashboardVo> dashboardVoList = new ArrayList<>();
        for (HotelRoomType hotelRoomType : hotelRoomTypeList) {
            DashboardVo dashboardVo = new DashboardVo();
            long count = this.checkInformationMapper.countByTypeId(hotelRoomType.getRoomTypeId());
            String name = hotelRoomTypeMapper.queryById(hotelRoomType.getRoomTypeId()).getRoomGrade();
            dashboardVo.setValue(count);
            dashboardVo.setName(name);
            dashboardVoList.add(dashboardVo);

        }
        return dashboardVoList;
    }

}
